package controllers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import models.user.Customer;

import com.fbpost.util.Constant;
import com.fbpost.util.StringUtil;

import play.Play;
import play.data.Upload;
import play.data.validation.Email;
import play.data.validation.Required;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.Controller;

@Secure(all = true)
public class Register extends Controller {
	/**
	 * 注册界面
	 */
	public static void index() {
		String loginName = "";
		String fullName = "";
		String phoneNumber = "";
		String language = "";
		if (request.cookies.get("loginName") != null) {
			loginName = request.cookies.get("loginName").value;
		}
		if (request.cookies.get("fullName") != null) {
			fullName = request.cookies.get("fullName").value;
		}
		if (request.cookies.get("phoneNumber") != null) {
			phoneNumber = request.cookies.get("phoneNumber").value;
		}
		if (request.cookies.get("language") != null) {
			language = request.cookies.get("language").value;
		}
		if (StringUtil.isNull(language) || StringUtil.isEqual("null", language)) {
			// 先取是否已经有cookie 设置了 语言. 否则取请求头
			if (request.headers.get("Accept-Language") != null) {
				language = request.headers.get("Accept-Language").value();
			} else {
				language = "zh-CN";
			}
		}
		response.setCookie("language", language, "365d");
		Lang.set(language);
		render(loginName, fullName, phoneNumber, language);
	}

	/**
	 * 注册页改变language语言选项(仅注册页使用)
	 * 
	 * @param lang
	 */
	public static void registerLanguage(String language) {
		if (!StringUtil.isNull(language)) {
			Lang.set(language);
			response.setCookie("language", language, "365d");
		}
		index();
	}

	/**
	 * 保存用户注册
	 * 
	 * @param loginName
	 * @param loginPassword
	 * @param confirmedPassword
	 * @param userName
	 * @param phone
	 * @param email
	 * @param address
	 */
	public static void save(String loginName, String loginPassword, String fullName, String phoneNumber, String language) {
		//非空校验
		if (!StringUtil.isNull(language)) {
			Lang.set(language);
			response.setCookie("language", language, "365d");
		}
		boolean isError = false;
		if (StringUtil.isNull(loginName)) {
			// 请输入账号
			validation.addError("loginName", Messages.get("pleaseEnterUserName"));
			isError = true;
		}
		if (StringUtil.isNull(loginPassword)) {
			// 请输入密码
			validation.addError("loginPassword", Messages.get("pleaseEnterPassword"));
			isError = true;
		}
		if (StringUtil.isNull(fullName)) {
			// 请输入姓名
			validation.addError("fullName", Messages.get("pleaseEnterFullName"));
			isError = true;
		}
		if (StringUtil.isNull(phoneNumber)) {
			// 请输入电话
			validation.addError("phoneNumber", Messages.get("pleaseEnterPhoneNumber"));
			isError = true;
		}
		if (isError) {
			render("@index", loginName, fullName, phoneNumber, language);
		}
		//长度校验
		if (loginName.length() > Constant.CUSTOMER_INFO_LENGTH) {
			validation.addError("loginName", Messages.get("tooLong30"));
			isError = true;
		}
		if (loginPassword.length() > Constant.CUSTOMER_INFO_LENGTH) {
			validation.addError("loginPassword", Messages.get("tooLong30"));
			isError = true;
		}
		if (fullName.length() > Constant.CUSTOMER_INFO_LENGTH) {
			validation.addError("fullName", Messages.get("tooLong30"));
			isError = true;
		}
		if (phoneNumber.length() > Constant.CUSTOMER_INFO_LENGTH) {
			validation.addError("phoneNumber", Messages.get("tooLong30"));
			isError = true;
		}
		if (isError) {
			render("@index", loginName, fullName, phoneNumber, language);
		}
		//保存用户输入
		response.setCookie("loginName", loginName, "31d");
		response.setCookie("phoneNumber", phoneNumber, "31d");
		response.setCookie("fullName", fullName, "31d");
		//信息校验
		Customer customer = Customer.findCustomerByLoginName(loginName);
		if (customer != null) {
			validation.addError("loginName", Messages.get("alreadyExist"));
			render("@index", loginName, fullName, phoneNumber, language);
		}

		//经过验证,进行保存
		Customer newCustomer = new Customer();
		newCustomer.loginName = loginName;
		newCustomer.loginPassword = loginPassword;
		newCustomer.createdTime = new Date();
		newCustomer.fullName = fullName;
		newCustomer.phoneNumber = phoneNumber;
		newCustomer.language = language;
		newCustomer.status = Constant.CUSTOMER_STATUS_UN_AUDITED;
		newCustomer.save();
		success();
	}

	public static void success() {
		String language = "";
		if (request.cookies.get("language") != null) {
			language = request.cookies.get("language").value;
			Lang.set(language);
		}
		render();
	}
}
